<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资产编码" prop="assetCode">
        <el-input v-model="queryParams.assetCode" placeholder="请输入资产编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="资产名称" prop="assetName">
        <el-input v-model="queryParams.assetName" placeholder="请输入资产名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="资产类别" prop="assetClass">
        <el-select v-model="queryParams.assetClass" placeholder="请选择资产类别" clearable>
          <el-option v-for="dict in dict.type.asset_class" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="管理员" prop="assetAdmin">
        <el-input v-model="queryParams.assetAdmin" placeholder="请输入管理员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="使用人" prop="assetUser">
        <el-input v-model="queryParams.assetUser" placeholder="请输入使用人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="购入时间" prop="assetBuy">
        <el-date-picker clearable v-model="queryParams.assetBuy" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择购入时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="区域" prop="assetArea">
        <el-select v-model="queryParams.assetArea" placeholder="请选择区域" clearable>
          <el-option v-for="dict in dict.type.asset_area" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="金额" prop="assetMoney">
        <el-input v-model="queryParams.assetMoney" placeholder="请输入金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="来源" prop="assetSource">
        <el-select v-model="queryParams.assetSource" placeholder="请选择来源" clearable>
          <el-option v-for="dict in dict.type.asset_source" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="资产状态" prop="assetStatus">
        <el-select v-model="queryParams.assetStatus" placeholder="请选择资产状态" clearable>
          <el-option v-for="dict in dict.type.asset_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['asset:asset:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['asset:asset:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['asset:asset:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['asset:asset:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资产编码" align="center" prop="assetCode" />
      <!-- <el-table-column label="id" align="center" prop="aId" /> -->
      <el-table-column label="资产名称" align="center" prop="assetName" />
      <el-table-column label="资产类别" align="center" prop="assetClass">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_class" :value="scope.row.assetClass" />
        </template>
      </el-table-column>
      <el-table-column label="管理员" align="center" prop="assetAdmin" />
      <el-table-column label="使用人" align="center" prop="assetUser" />
      <el-table-column label="购入时间" align="center" prop="assetBuy" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assetBuy, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" align="center" prop="assetCompany" />
      <el-table-column label="使用公司" align="center" prop="assetUseCompany" />
      <el-table-column label="使用部门" align="center" prop="assetDepartment" />
      <el-table-column label="区域" align="center" prop="assetArea">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_area" :value="scope.row.assetArea" />
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="assetModel" />
      <el-table-column label="计量单位" align="center" prop="assetUnit" />
      <el-table-column label="金额" align="center" prop="assetMoney" />
      <el-table-column label="来源" align="center" prop="assetSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_source" :value="scope.row.assetSource" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="assetRemark" />
      <el-table-column label="资产状态" align="center" prop="assetStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_status" :value="scope.row.assetStatus" />
        </template>
      </el-table-column>
      <el-table-column label="资产照片" align="center" prop="assetImages" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.assetImages" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:asset:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['asset:asset:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改资产列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资产编码" prop="assetCode">
          <el-input v-model="form.assetCode" placeholder="请输入资产编码" />
        </el-form-item>
        <el-form-item label="资产名称" prop="assetName">
          <el-input v-model="form.assetName" placeholder="请输入资产名称" />
        </el-form-item>
        <el-form-item label="资产类别" prop="assetClass">
          <el-select v-model="form.assetClass" placeholder="请选择资产类别">
            <el-option v-for="dict in dict.type.asset_class" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属公司" prop="assetCompany">
          <treeselect v-model="form.assetCompany" :options="assetCompanyOptions" :show-count="true"
            placeholder="请选择所属公司" />
        </el-form-item>
        <el-form-item label="使用公司" prop="assetUseCompany">
          <treeselect v-model="form.assetUseCompany" :options="assetUseCompanyOptions" :show-count="true"
            placeholder="请选择所属公司" />
        </el-form-item>
        <el-form-item label="使用部门" prop="assetDepartment">
          <treeselect v-model="form.assetDepartment" :options="deptOptions" :show-count="true" placeholder="请选择使用部门" />
        </el-form-item>

        <el-form-item label="管理员" prop="assetAdmin">
          <el-select v-model="form.assetAdmin" class="m-2" placeholder="请选择管理员" size="large">
            <el-option v-for="(item, index) in assetAdminOptions" :key="index" :label="item.userName"
              :value="item.userName" />
          </el-select>
        </el-form-item>

        <el-form-item label="使用人" prop="assetUser">
          <!-- <el-input v-model="form.assetUser" placeholder="请输入使用人" /> -->
          <el-select v-model="form.assetUser" class="m-2" placeholder="请选择使用人" size="large">
            <el-option v-for="(item, index) in assetUserOptions" :key="index" :label="item.userName"
              :value="item.userName" />
          </el-select>
        </el-form-item>

        <el-form-item label="购入时间" prop="assetBuy">
          <el-date-picker clearable v-model="form.assetBuy" type="date" value-format="yyyy-MM-dd" placeholder="请选择购入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="区域" prop="assetArea">
          <el-select v-model="form.assetArea" placeholder="请选择区域">
            <el-option v-for="dict in dict.type.asset_area" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号" prop="assetModel">
          <el-input v-model="form.assetModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="计量单位" prop="assetUnit">
          <el-input v-model="form.assetUnit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="金额" prop="assetMoney">
          <el-input v-model="form.assetMoney" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="来源" prop="assetSource">
          <el-select v-model="form.assetSource" placeholder="请选择来源">
            <el-option v-for="dict in dict.type.asset_source" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="assetRemark">
          <el-input v-model="form.assetRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="资产状态" prop="assetStatus">
          <el-radio-group v-model="form.assetStatus">
            <el-radio v-for="dict in dict.type.asset_status" :key="dict.value" :label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资产照片" prop="assetImages">
          <image-upload v-model="form.assetImages" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAsset, getAsset, delAsset, addAsset, updateAsset, deptTreeSelect } from "@/api/asset/asset";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Asset",
  dicts: ['asset_area', 'asset_class', 'asset_source', 'asset_status'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //管理员选项
      assetAdminOptions: [],
      //用户选项
      assetUserOptions: [],
      // 部门树选项
      deptOptions: [],
      //所属公司
      assetCompanyOptions: [],
      //使用公司
      assetUseCompanyOptions: [],
      // 资产列表表格数据
      assetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetCode: null,
        assetName: null,
        assetClass: null,
        assetAdmin: null,
        assetUser: null,
        assetBuy: null,
        assetCompany: null,
        assetUseCompany: null,
        assetDepartment: null,
        assetArea: null,
        assetMoney: null,
        assetSource: null,
        assetStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assetCode: [
          { required: true, message: "资产编码不能为空", trigger: "blur" }
        ],
        assetName: [
          { required: true, message: "资产名称不能为空", trigger: "blur" }
        ],
        assetClass: [
          { required: true, message: "资产类别不能为空", trigger: "change" }
        ],
        assetBuy: [
          { required: true, message: "购入时间不能为空", trigger: "blur" }
        ],
        assetCompany: [
          { required: true, message: "所属公司不能为空", trigger: "change" }
        ],
        assetUseCompany: [
          { required: true, message: "使用公司不能为空", trigger: "change" }
        ],
        assetDepartment: [
          { required: true, message: "使用部门不能为空", trigger: "change" }
        ],
        assetStatus: [
          { required: true, message: "资产状态不能不填写", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询资产列表列表 */
    getList() {
      this.loading = true;
      listAsset(this.queryParams).then(response => {
        this.assetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询使用部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
        this.assetCompanyOptions = response.data;
        this.assetUseCompanyOptions = response.data;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        assetCode: null,
        aId: null,
        assetName: null,
        assetClass: null,
        assetAdmin: null,
        assetUser: null,
        assetBuy: null,
        assetCompany: null,
        assetUseCompany: null,
        assetDepartment: null,
        assetArea: null,
        assetModel: null,
        assetUnit: null,
        assetMoney: null,
        assetSource: null,
        assetRemark: null,
        assetStatus: null,
        assetImages: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.aId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset();
      // const aId = '1'
      await getAsset('1').then(response => {
        // this.form = response.data;
        this.assetAdminOptions = response.assetAdmins;
        this.assetUserOptions = response.assetUsers;

        this.open = true;
        this.title = "添加资产列表";
      })
      this.getDeptTree();
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const aId = row.aId || this.ids
      await getAsset(aId).then(response => {
        this.form = response.data;

        this.assetAdminOptions = response.assetAdmins;
        this.assetUserOptions = response.assetUsers;

        this.open = true;
        this.title = "修改资产列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.aId != null) {
            updateAsset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAsset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.assetAdminOptions = response.assetAdmins;
              this.assetUserOptions = response.assetUsers;

              let da = response.assetAdmins;
              console.log(da)
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const aIds = row.aId || this.ids;
      this.$modal.confirm('是否确认删除资产列表编号为"' + aIds + '"的数据项？').then(function () {
        return delAsset(aIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/asset/export', {
        ...this.queryParams
      }, `asset_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
